package com.ywgt.service.impl;

import com.ywgt.pojo.GoodsSpecification;
import com.ywgt.mapper.GoodsSpecificationMapper;
import com.ywgt.service.GoodsSpecificationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Data;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品规格表 服务实现类
 * </p>
 *
 * @author yiping
 * @since 2021-03-13
 */
@Service
public class GoodsSpecificationServiceImpl extends ServiceImpl<GoodsSpecificationMapper, GoodsSpecification> implements GoodsSpecificationService {

    @Override
    public List getByGoodId(Integer id) {
        Map<String,Object> searchMap=new HashMap<>();
        searchMap.put("goods_id",id);
        List<GoodsSpecification> specifications = this.listByMap(searchMap);
        Map<String,VO> data=new HashMap<>();
        List<VO> vos=new ArrayList<>();
        for (GoodsSpecification specification : specifications) {
            String key = specification.getSpecification();
            VO vo = data.get(key);
            if(vo==null){
                String vkey = specification.getSpecification();
                vo=new VO();
                vo.setName(vkey);
                List<GoodsSpecification> goodsSpecifications=new ArrayList<>();
                goodsSpecifications.add(specification);
                vo.setValueList(goodsSpecifications);
                data.put(vkey,vo);
                vos.add(vo);
            }else {
                vo.getValueList().add(specification);
            }
        }

        return vos;
    }


    @Data
    private class VO {
        private String name;
        private List<GoodsSpecification> valueList;
    }
}
